#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
This module solves apparent dip problem for linetion.
The attitude of the line can be given as the lunge angle or the pitch angle and
as the trend angle. Default is the pitch angle. The module can find the trend 
angle for line, or the plunge angle, or the pitch angle. Default is trend angle.
"""

import sys
from optparse import OptionParser
from math import tan, cos, atan, acos, radians, degrees

def line_parse(line):
    line = line.expandtabs(1)
    line = [x for x in line.split(' ') if x != '' ]    
    # the format of the string to parse is
    # DIR DIP LINE "TR" looks col,col,col
    DIP, DIR, LINE = 0, 0, 0    
    
    try:
        DIR, DIP, LINE = int(line.pop(0)), int(line.pop(0)), int(line.pop(0))
    except:
        #sys.stdout.write('WARNING! Cannot parse string: %s\n' % line)
        return None   
    line = [DIR, DIP, LINE]
    return line

# Parsing of an options
optparser= OptionParser()
optparser.add_option('-i', '--in attitude', dest='iattitude',
                     help='The angle of attitude for input planes. TR for trend, \
                      PL for plunge, PT for pitch. Default is PT',
                     default='PT')
optparser.add_option('-o', '--out attitude', dest='oattitude',
                     help='The angle of attitude for output planes. TR for trend, \
                      PL for plunge, PT for pitch. Default is PL',
                     default='TR')                     
options, args = optparser.parse_args(sys.argv[1:])

lines = sys.stdin.read().split('\n')

# parsing of the data
data=[]
for line in lines:
    val = line_parse(line)
    if val != None: data.append(val)  

# the work itself
if options.iattitude == 'PT':
    for i in range(len(data)):
        # tan (delta) = tg (Pitch) / cos (Dip)        
        data[i][2] = data[i][0] + degrees(atan(tan(radians(data[i][2])) \
                                          /cos(radians(data[i][1]))))                                
elif options.iattitude == 'PL':
    for i in range(len(data)):
        # cos (delta) = tan(Dip_) / tan (Dip)
        print data[i]  
        if data[i][1] == 0:
            data[i] = None
            continue
        if data[i][2]> data[i][1]:
            data[i] = None
            continue
        if data[i][2] == 90:
            data[i][2] = data[i][0]
            continue
            
        data[i][2] = data[i][0] + degrees(acos(tan(radians(data[i][2])) \
                                         / tan(radians(data[i][1]))))
elif options.iattitude == 'TR':
    pass

if options.oattitude == 'PT':
    pass
elif options.oattitude == 'PL':
    # tan (Dip_) = cos (delta) * tan (Dip)
    pass
elif options.oattitude == 'TR':
    for line in data:
        if line != None:
            sys.stdout.write('%003d\t%02d\t%003d\n' % (line[0], line[1], line[2]))        
